Have you ever had the experience of walking down the street and wondering, “What’s the difference between a compiled language and an interpreted one?” It surely happens to you, it’s a very common question. And if not, well… what are you doing here? Go live your life, you’ve got it all figured out.
It’s important to know that even though we commonly talk about programming languages as either interpreted or compiled, in reality, languages can behave in either way. They don’t really care. Many of them are normally used in a specific way, and that’s why people forget they could be used differently.
But hey, most of the time it doesn’t really matter. When you program, you stick to the behavior of the tools, and you rarely need to deviate from the standard.
That being said, it’s important for you to understand the difference because it will make you a better developer.
Compiled
A compiled language transforms your source code into binary code. The purpose of that binary code is to be executed directly by a specific platform.
- Source Code to binary code
- The instructions in binary code are read and executed.
Pros
1. It’s faster
At least compared to an interpreted language.
This is because computers love binary language, and if you give them a bunch of binary, the computer just executes everything that the file says… without thinking or anything… like a drone in this society. 🙁
2. The source code is private
This is because the code given to the computer is binary. So only a robot could read it. A human wouldn’t be able to understand a binary file… Or can they?
Cons
1. Requires recompilation for each platform
The program has to be recompiled for each platform on which it is intended to run. Generally, for each combination of operating system and CPU.
If you’re launching your app for PCs, phones, and refrigerators, you have to recompile everything many times and be very organized to avoid mixing them up. And well, that doesn’t sound difficult, but it’s laborious, and we developers don’t like that.
2. It’s slower to debug
To test that everything works, you have to recompile all the code every time a change is made. Programmers tend to make many changes, so this becomes tiresome.
Interpeted
An interpreted language doesn’t transform anything initially. It waits for the source code to be passed to the platforms that will execute it and these platforms “interpret” the code, transforming it into binary code as they process it.
- The source code is read, interpreted, and executed.
- At each reading step, the binary code is created. This code is then is used to execute the instructions.
Pros
1. Easy to deploy
You don’t have to compile a specific version for each platform.
All you have to do is give your source code to all platforms, and that’s it. You don’t need to prepare one for the fridge and another for the smartwatch. It would run the same everywhere.
2. Easy to debug
Since you don’t have to recompile the entire code to run it, the debugging process is much faster compared to a compiled language.
Cons
1. It’s slower
It’s slower to execute compared to a compiled language.
This is because the platforms where it runs have to convert the source code to binary code as they execute it.
This may not be clear to some project managers (PMs). But basically, it’s the concept that…if you give someone more work…it takes longer for them to finish it.
2. The source code is public
Obviously, if you pass the source code to each platform, then everyone has access to it.
This is extremely important to remember. That’s why sometimes you come across jokes or variable names with… inappropriate names on public websites.
BONUS
Intermediate Language and JiT
If you’re in the mood, if you still have room for a little more information… well, keep reading.
It would be good for you to know that solutions have been created to optimize interpreted languages, which, in a very general way, consist of adding a step before generating the binary code.
- Compile the code to an Intermediate Language (IL) or Bytecode.
- Compile the IL or Bytecode to binary code. This can be done with a virtual machine like JVM, a Just-in-Time (JiT) compiler, etc.
- The code is compiled to IL or Bytecode.
- The generated file is read, compiled again, and executed.
- At each reading step, the binary code is created. The code is then used to execute the instructions.
I won’t delve further into this topic because it’s not the question that arose while you were walking down the street. But it’s good for you to know that it exists. And who knows… maybe someday the universe will inspire you to learn more about it.